﻿using Direct.IDAL.Manager.ServiceCenter;
using DirectSell.Common.Url;
using DirectSell.Model.Query;
using DirectSell.Model.User;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.DAL.Manager.ServiceCenter
{
    /// <summary>
    /// 服务中心审核仓储层
    /// </summary>
    public class ServiceCenterAuditRepository:BaseManagerRepository, IServiceCenterAuditRepository
    {
        #region 查询待审核列表
        /// <summary>
        /// 查询待审核列表
        /// </summary>
        /// <returns></returns>
        public async Task<PagedList<UserInforMationDetails>> QueryList(BaseCondition condition)
        {
            PageParam param = new PageParam();
            param.OrderBy = "UserId Desc";
            param.PageSize = condition.pageSize;
            param.TableName = "UserInforMations";
            param.PageIndex = condition.pageIndex == 0 ? 1 : condition.pageIndex;
            StringBuilder sb = new StringBuilder();
            sb.Append("  (IsCenterService =2 OR IsCenterService=1 )  ");


            if (condition.SerachCondition != null)
            {
                sb.AppendFormat("AND (UserName LIKE '%{0}%' OR UserCode  LIKE'%{0}%' OR Phone LIKE '%{0}%') ", condition.SerachCondition.Trim());
            }

            if (!string.IsNullOrEmpty(condition.startTime))
            {
                if (!string.IsNullOrEmpty(condition.endTime))
                {
                    sb.AppendFormat(" AND RegistionTime>='{0}' and RegistionTime<='{1}' ", condition.startTime, condition.endTime);
                }
                else
                {
                    sb.AppendFormat(" AND RegistionTime>='{0}' and RegistionTime<='{1}' ", condition.startTime, DateTime.Now.ToString());
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(condition.endTime))
                {
                    sb.AppendFormat(" AND RegistionTime>='2010-01-01' and RegistionTime<='{1}' ", condition.endTime);
                }
            }
            param.StrWhere = sb.ToString();
           // sb.Append(" ORDER BY IsCenterService DESC");
            return await base.PageQuery<UserInforMationDetails>(param);
        }
        #endregion

        #region 审核
        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="usercode"></param>
        public void Audit(string usercode,int type, ref Hashtable hstable)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("UPDATE UserInforMations SET IsCenterService={0} WHERE UserCode='{1}'",type,usercode);
            hstable.Add(sb.ToString(), null);
            
        }
        #endregion

        #region 审核不通过删除记录
        /// <summary>
        /// 审核不通过删除记录
        /// </summary>
        /// <param name="usercode"></param>
        public void DelectRecord(string usercode,ref Hashtable hstable)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" DELETE FROM ServiceCentre WHERE ServiceCenterCode='{0}' ",usercode);
            hstable.Add(sb.ToString(), null);
        }
        #endregion

        #region 查询服务中心信息
        /// <summary>
        /// 查询服务中心信息 
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public Model.ServiceCenter.ServiceCenter QueryCenter(string usercode)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" SELECT * from ServiceCentre WHERE ServiceCenterCode='{0}' ", usercode);
            return base.Query<Model.ServiceCenter.ServiceCenter>(sb.ToString()).FirstOrDefault();
        }
        #endregion

    }
}
